﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SLogo.Compile
{
    /// <summary>
    /// 变量的结构
    /// </summary>
    class Variable
    {
        /// <summary>
        /// 变量名
        /// </summary>
        public String Name { get; set; }
        /// <summary>
        /// 字符串类型变量的值
        /// </summary>
        public String StringValue
        {
            get
            {
                return TheType == VariableType.String ? _stringValue : null;
            }
            set { _stringValue = value; }
        }
        //真正存储值的字符串
        private String _stringValue;
        /// <summary>
        /// 数值类型变量的值
        /// </summary>
        public double NumberValue
        {
            get
            {
                return TheType == VariableType.Number ? _numberValue : Double.NaN;
            }
            set { _numberValue = value; }
        }
        //真正存储值的数值变量
        private double _numberValue;
        /// <summary>
        /// 变量类型
        /// </summary>
        public VariableType TheType { get; set; }
        /// <summary>
        /// 变量类型的枚举
        /// </summary>
        public enum VariableType
        {
            String,
            Number
        }
        /// <summary>
        /// 初始化为字符串
        /// </summary>
        /// <param name="name">变量名</param>
        /// <param name="value">变量值</param>
        public Variable(String name, String value)
        {
            Name = name;
            StringValue = value;
            TheType = VariableType.String;
        }
        /// <summary>
        /// 初始化为数值
        /// </summary>
        /// <param name="name">变量名</param>
        /// <param name="value">变量值</param>
        public Variable(String name, double value)
        {
            Name = name;
            NumberValue = value;
            TheType = VariableType.Number;
        }
        /// <summary>
        /// 初始化为未知类型
        /// </summary>
        /// <param name="name">变量名</param>
        public Variable(String name)
        {
            Name = name;
        }
        /// <summary>
        /// 更新为字符串
        /// </summary>
        /// <param name="value">字符串值</param>
        public void Update(String value)
        {
            StringValue = value;
            TheType = VariableType.String;
        }
        /// <summary>
        /// 更新为数值
        /// </summary>
        /// <param name="value">数值</param>
        public void Update(double value)
        {
            NumberValue = value;
            TheType = VariableType.Number;
        }
    }
}
